package com.lanlian.cayman.activity;


import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.data.ContractBean;
import com.lanlian.commonlib.data.MarketBean;
import com.lanlian.commonlib.data.RespMarket;
import com.lanlian.cayman.HqMsg;
import com.lanlian.cayman.R;
import com.lanlian.cayman.bean.Market;
import com.lanlian.cayman.widget.KlineView;
import com.oms.rpc.protobuf.KLineHqResp;
import com.oms.rpc.protobuf.OMSTradeReq;
import com.oms.rpc.protobuf.OMSTradeRsp;
import com.oms.rpc.protobuf.TickHqReq;
import com.oms.rpc.protobuf.TickHqResp;
import com.oms.rpc.protobuf.YsHqInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 大盘
 */


public class KLineActivityBase extends BaseActivity implements OnClickListener
{
    TextView tv_ma;
    TextView tv_ema;
    TextView tv_boll;
    TextView tv_vol;
    TextView tv_macd;
    TextView tv_kdj;
    TextView tv_rsi;
    TextView tv_roc;

   public static  int klineType=0;
    static  int selectType=0;
    static  int zhibiaoTop=0;
    static  int zhibiao=0;
    public static   String name;
    //合约id
    public static String id;

    public KlineView kline;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setRequestedOrientation() {

    }

    public void reZhibiao()
    {
        int color=getResources().getColor(R.color.color_text6);
        kline.setType(klineType,zhibiaoTop);
        if(klineType!=0) {
            if (zhibiaoTop == 0) {
                kline.kLineView.showMA();
                kline.setTopZhiBiao(1);
                initzhiBiaoUp();
                tv_ma.setTextColor(color);
            } else if (zhibiaoTop == 1) {
                kline.kLineView.showEMA();
                kline.setTopZhiBiao(2);
                initzhiBiaoUp();
                tv_ema.setTextColor(color);
            } else if (zhibiaoTop == 2) {
                kline.kLineView.showBOLL();
                kline.setTopZhiBiao(3);
                initzhiBiaoUp();
                tv_boll.setTextColor(color);
            }
        }
        else
        {

        }
        if(zhibiao==0)
        {
            kline.kLineView.showVOL();
            initZhiBiao();
            tv_vol.setTextColor(color);
        }
        else if(zhibiao==1)
        {
            kline.kLineView.showMACD();
            initZhiBiao();
            tv_macd.setTextColor(color);
        }
        else if(zhibiao==2)
        {


            kline.kLineView.showKDJ();
            initZhiBiao();
            tv_kdj.setTextColor(color);
        }
        else if(zhibiao==3)
        {
            kline.kLineView.showRSI();
            initZhiBiao();
            tv_rsi.setTextColor(color);
        }
        else if(zhibiao==4)
        {
            kline.kLineView.showROC();
            initZhiBiao();
            tv_roc.setTextColor(color);
        }

    }
    @Override
    public void onClick(View v) {
        int color=getResources().getColor(R.color.color_text6);
           switch (v.getId())
           {
               case R.id.tv_kdj:
                   zhibiao=2;
                   kline.kLineView.showKDJ();
                   initZhiBiao();
                   tv_kdj.setTextColor(color);
                   break;
               case R.id.tv_macd:
                   zhibiao=1;
                   kline.kLineView.showMACD();
                   initZhiBiao();
                   tv_macd.setTextColor(color);
                   break;
               case R.id.tv_vol:
                   zhibiao=0;
                   kline.kLineView.showVOL();
                   initZhiBiao();
                   tv_vol.setTextColor(color);
                   break;
               case R.id.tv_boll:
                   zhibiaoTop=2;
                   kline.kLineView.showBOLL();
                   kline.setTopZhiBiao(3);
                   initzhiBiaoUp();
                   tv_boll.setTextColor(color);
                   break;
               case R.id.tv_rsi:
                   zhibiao=3;
                   kline.kLineView.showRSI();
                   initZhiBiao();
                   tv_rsi.setTextColor(color);
                   break;
               case R.id.tv_roc:
                   zhibiao=4;
                   kline.kLineView.showROC();
                   initZhiBiao();
                   tv_roc.setTextColor(color);
                   break;
               case R.id.tv_ma:
                   zhibiaoTop=0;
                   kline.kLineView.showMA();
                   kline.setTopZhiBiao(1);
                   initzhiBiaoUp();
                   tv_ma.setTextColor(color);
                   break;
               case R.id.tv_ema:
                   zhibiaoTop=1;
                   kline.kLineView.showEMA();
                   kline.setTopZhiBiao(2);
                   initzhiBiaoUp();
                   tv_ema.setTextColor(color);
                   break;
           }
    }
    public void nextZhiBiao()
    {
        int color=getResources().getColor(R.color.color_text6);
        if(zhibiao==0)
        {
            zhibiao=1;
            kline.kLineView.showMACD();
            initZhiBiao();
            tv_macd.setTextColor(color);
        }
        else if(zhibiao==1)
        {
            zhibiao=2;
            kline.kLineView.showKDJ();
            initZhiBiao();
            tv_kdj.setTextColor(color);
        }
        else if(zhibiao==2)
        {
            zhibiao=3;
            kline.kLineView.showRSI();
            initZhiBiao();
            tv_rsi.setTextColor(color);
        }
        else if(zhibiao==3)
        {
            zhibiao=4;
            kline.kLineView.showROC();
            initZhiBiao();
            tv_roc.setTextColor(color);
        }
        else if(zhibiao==4)
        {
            zhibiao=0;
            kline.kLineView.showVOL();
            initZhiBiao();
            tv_vol.setTextColor(color);
        }
    }
    private void initzhiBiaoUp()
    {
        int color=getResources().getColor(R.color.color_text5);
        tv_ma.setTextColor(color);
        tv_ema.setTextColor(color);
        tv_boll.setTextColor(color);
    }
    private void initZhiBiao()
    {
        int color=getResources().getColor(R.color.color_text5);
        tv_vol.setTextColor(color);
        tv_macd.setTextColor(color);
        tv_kdj.setTextColor(color);
        tv_rsi.setTextColor(color);
        tv_roc.setTextColor(color);

    }

    private ArrayList<Market> SwitchData(RespMarket respMarket)
    {
        ArrayList<Market> list=new ArrayList<>();
        if(respMarket==null)
        {
            return list;
        }
        Market mk=null;

        ArrayList<MarketBean> marketList=respMarket.getMarketList();
        ArrayList<ContractBean> contractList=respMarket.getContractList();
        for(int i=0;i<marketList.size();i++)
        {
            mk=new Market();
               mk.name=marketList.get(i).marketName;
               for(int a=0;a<contractList.size();a++)
               {
                   if(marketList.get(i).id.equals(contractList.get(a).marketId))
                   {
                       mk.treadys.add(contractList.get(a).contractName);
                       mk.treadys_id.add(contractList.get(a).contractNo);
                   }
               }
               if(mk.treadys.size()>0) {
                   list.add(mk);
               }
        }
        return list;

    }

    /**
     * 判断某个Activity 界面是否在前台
     * @param context
     * @param className 某个界面名称
     * @return
     */
    public static boolean  isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className)) {
            return false;
        }
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;
            }
        }
        return false;
    }
}
